package com.qffx.moviebase.view;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.qffx.moviebase.R;
import com.qffx.moviebase.adapter.find.FindFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends Fragment implements View.OnClickListener{
    private View view;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> fragment_list;
    private FindFragmentAdapter adapter;
    private ImageView find_post;
    private String [] arr={"最新","关注"};
    public FindFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_find, container, false);
        init();


        return view;
    }

    private void init() {
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        find_post= (ImageView) view.findViewById(R.id.find_post);
        find_post.setOnClickListener(this);
        fragment_list=new ArrayList<>();
        adapter=new FindFragmentAdapter(getChildFragmentManager(),fragment_list,arr);
        viewPager.setAdapter(adapter);
        initTabLayout();
        initFragment();
    }

    private void initFragment() {
        FindNeweatFragment nFragment=new FindNeweatFragment();
        FindFollowFragment fFragment=new FindFollowFragment();
        fragment_list.add(nFragment);
        fragment_list.add(fFragment);
        adapter.notifyDataSetChanged();

    }

    private void initTabLayout() {
        //设置标题的字体颜色，正常和选中
        tabLayout.setTabTextColors(Color.BLACK, Color.BLUE);
        //设置小滑块的高度
        tabLayout.setSelectedTabIndicatorHeight(3);
        //设置小滑块的颜色
        tabLayout.setSelectedTabIndicatorColor(Color.BLUE);

        //设置顶部导航的背景色
        tabLayout.setBackgroundColor(Color.WHITE);
        //设置关联的ViewPager对象
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                viewPager.setCurrentItem(tab.getPosition(), true);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        Toast.makeText(getActivity(), "aaaa", Toast.LENGTH_SHORT).show();
    }
}
